/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.MonthlyRevenueEntity;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import type.Month;

/**
 *
 * @author wei
 */
@Stateless
public class MonthlyRevenueEntityFacade extends AbstractFacade<MonthlyRevenueEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public MonthlyRevenueEntityFacade() {
        super(MonthlyRevenueEntity.class);
    }

    public MonthlyRevenueEntity findByYearMonth(int year, Month month) {
        try {
            Query query = em.createQuery("SELECT s FROM MonthlyRevenue s "
                    + " WHERE s.archived != 'true' AND s.yearOfCentury=:yearOfCentury AND s.monthOfYear=:monthOfYear");
            query.setParameter("yearOfCentury", year);
            query.setParameter("monthOfYear", month);

            return (MonthlyRevenueEntity) query.getSingleResult();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    
    public void clearRecordForYear(int year){
        for(Month m : Month.values()){
            MonthlyRevenueEntity revenue = findByYearMonth(year, m);
            if(revenue!=null)
                originalRemove(revenue);
        }
    }

}
